package org.spring.springboot.app.file.controller;

import cn.novelweb.tool.upload.local.pojo.UploadFileParam;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.ApiIndex;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.file.domain.vo.FileAddVO;
import org.spring.springboot.app.file.domain.vo.FileSearchVO;
import org.spring.springboot.app.file.domain.vo.FileVO;
import org.spring.springboot.app.file.service.IFileService;
import org.spring.springboot.util.ObsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

import static cn.novelweb.tool.upload.qcloud.CosUpload.appId;


/**
 * @ClassName FileController
 * @Author HandsomeBugs
 * @Date 2019/7/9 17:04
 * @Version 1.0
 **/

@Api(tags = ApiIndex.FileController)
@RequestMapping(value = "/api/file")
@RestController
public class FileController {

    @Autowired
    private IFileService service;

    @ApiOperation(value = "文件上传【华为云OBS储存】")
    @PostMapping(value = "/upload_obs")
    @ResponseBody
    public R<FileVO> upload_obs(
            @ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId
    ) {
        return service.upload_obs(file, appId);
    }


//    @ApiOperation(value = "测试文件上传【华为云OBS储存】")
//    @PostMapping(value = "/upload_obs_s")
//    @ResponseBody
//    public R<FileVO> upload_obs_s(
//            @ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
//            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId
//    ) {
//        return service.upload_obs_s(file, appId);
//    }



    @ApiOperation(value = "文件上传【本地存储】")
    @PostMapping(value = "/upload")
    @ResponseBody
    public R<FileVO> upload(
            @ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId
    ) {
        return service.upload(file, appId);
    }



    @ApiOperation(value = "检查文件【分片上传】")
    @GetMapping(value = "/check_file")
    public R checkFileMd5(
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId,
            @ApiParam(value = "文件MD5", required = true) @RequestParam(defaultValue = "") String md5,
            @ApiParam(value = "文件名称", required = true) @RequestParam(defaultValue = "") String fileName,
            @ApiParam(value = "上传类型（1华为云OBS 2本地储存）", required = true) @RequestParam(defaultValue = "1") Integer type
            ) throws Exception {
        return service.checkFileMd5(appId,md5,fileName,type);
    }


    @ApiOperation(value = "文件上传完成【分片上传】")
    @PostMapping(value = "/success_file")
    public R successFile(
            @ApiParam(value = "参数") @Valid @RequestBody  FileAddVO fileAddVO
    ) throws Exception {
        return service.successFile(fileAddVO.getAppId(), fileAddVO.getMd5(),fileAddVO.getType(),fileAddVO.getFileName(),fileAddVO.getFileSize());
    }


    @ApiOperation(value = "文件分片上传【本地存储】")
    @PostMapping(value = "/upload_shard")
    @ResponseBody
    public R upload_shard(
            UploadFileParam uploadFileParam,
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId,
            HttpServletRequest request
    ) {
        return service.upload_shard(uploadFileParam,appId,request);
    }

    @ApiOperation(value = "文件分片上传【华为云OBS储存】")
    @PostMapping(value = "/upload_shard_obs")
    @ResponseBody
    public R upload_shard_obs(
            UploadFileParam uploadFileParam,
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId,
            HttpServletRequest request
    ) throws Exception {
        return service.upload_shard_obs(uploadFileParam,appId,request);
    }


    @ApiOperation(value = "删除视频资源【华为云OBS储存】")
    @PostMapping(value = "/delete_file")
    @ResponseBody
    public R delete_obs(
            @ApiParam(value = "appId【内部接口】", required = true) @RequestParam(defaultValue = "") String appId,
            @ApiParam(value = "url") @RequestParam(defaultValue = "") String url
    ) throws Exception {
        return service.delete_file(appId,url);
    }


    @ApiOperation(value = "视频文件下载")
    @GetMapping(value = "/download")
    @ResponseBody
    public R download(
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId,
            @ApiParam(value = "url", required = true) @RequestParam(defaultValue = "") String url,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        return service.download(appId,url,request,response);
    }

    @ApiOperation(value = "视频文件下载【华为云OBS储存】")
    @GetMapping(value = "/download_obs")
    @ResponseBody
    public R download_obs(
            @ApiParam(value = "appId【商户传商户ID 运营中心-888888 后台管理-999999】", required = true) @RequestParam(defaultValue = "") String appId,
            @ApiParam(value = "url", required = true) @RequestParam(defaultValue = "") String url,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        return service.download_obs(appId,url,request,response);
    }


    @ApiOperation(value = "视频文件下载【华为云OBS储存】")
    @GetMapping(value = "/video_download")
    @ResponseBody
    public R video_download(
            @ApiParam(value = "videoId", required = true) @RequestParam(defaultValue = "") String videoId,
            @ApiParam(value = "userId", required = true) @RequestParam(defaultValue = "") String userId,
            @ApiParam(value = "type【1 原视频 2 转码视频】", required = true) @RequestParam(defaultValue = "1") Integer type,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        return service.video_download(videoId,userId,type,request,response);
    }

    @ApiOperation(value = "（全部下载）剧目视频文件下载【华为云OBS储存】")
    @GetMapping(value = "/drama_video_download")
    @ResponseBody
    public R drama_video_download(
            @ApiParam(value = "dramaId", required = true) @RequestParam(defaultValue = "") String dramaId,
            @ApiParam(value = "userId", required = true) @RequestParam(defaultValue = "") String userId,
            @ApiParam(value = "type【1 原视频 2 转码视频】", required = true) @RequestParam(defaultValue = "1") Integer type,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        return service.drama_video_download(dramaId,userId,type,request,response);
    }




//
//    @ApiOperation(value = "删除文件")
//    @PostMapping(value = "/delete")
//    @ResponseBody
//    public R delete(
//            @ApiParam(value = "文件ID", required = true) @RequestParam(defaultValue = "") String fileId,
//            @ApiParam(value = "appId", required = true) @RequestParam(defaultValue = "") String appId
//    ) {
//        return service.delete(fileId, appId);
//    }


//    @ApiOperation(value = "根据路径删除文件")
//    @PostMapping(value = "/deletePath")
//    @ResponseBody
//    public R deletePath(
//            @ApiParam(value = "文件路径", required = true) @RequestParam(defaultValue = "") String filePath,
//            @ApiParam(value = "appId", required = true) @RequestParam(defaultValue = "") String appId
//    ) {
//        return service.deletePath(filePath, appId);
//    }


//    @ApiOperation(value = "查询文件")
//    @GetMapping(value = "/getOne")
//    @ResponseBody
//    public R<FileVO> getOne(
//            @ApiParam(value = "文件ID", required = true) @RequestParam(defaultValue = "") String fileId,
//            @ApiParam(value = "appId", required = true) @RequestParam(defaultValue = "") String appId
//    ) {
//        return service.getOne(fileId, appId);
//    }

    @ApiOperation(value = "查询文件列表")
    @GetMapping(value = "/getList")
    @ResponseBody
    public  R<PageInfo<List<FileVO>>> getList(
            @ApiParam(value = "查询参数") @ModelAttribute FileSearchVO searchVO
    ) {
        List<FileVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

}
